// Decompiled on 周六 2月 22 19:31:49 CST 2025 with Zomboid Decompiler v0.1.3 using Vineflower.
package zombie.network.packets.hit;

import java.nio.ByteBuffer;
import zombie.characters.IsoGameCharacter;
import zombie.characters.IsoPlayer;
import zombie.characters.IsoZombie;
import zombie.core.network.ByteBufferWriter;
import zombie.core.raknet.UdpConnection;
import zombie.debug.DebugLog;
import zombie.network.GameServer;
import zombie.network.packets.INetworkPacket;

public abstract class Hit implements INetworkPacket {
    private static final float MAX_DAMAGE = 100.0F;
    protected boolean ignore;
    protected float damage;
    protected float hitForce;
    protected float hitDirectionX;
    protected float hitDirectionY;

    public void set(boolean _ignore, float _damage, float _hitForce, float _hitDirectionX, float _hitDirectionY) {
        this.ignore = _ignore;
        this.damage = Math.min(_damage, 100.0F);
        this.hitForce = _hitForce;
        this.hitDirectionX = _hitDirectionX;
        this.hitDirectionY = _hitDirectionY;
    }

    @Override
    public void parse(ByteBuffer b, UdpConnection connection) {
        this.ignore = b.get() != 0;
        this.damage = b.getFloat();
        this.hitForce = b.getFloat();
        this.hitDirectionX = b.getFloat();
        this.hitDirectionY = b.getFloat();
    }

    @Override
    public void write(ByteBufferWriter b) {
        b.putBoolean(this.ignore);
        b.putFloat(this.damage);
        b.putFloat(this.hitForce);
        b.putFloat(this.hitDirectionX);
        b.putFloat(this.hitDirectionY);
    }

    @Override
    public String getDescription() {
        return "\n\tHit [ ignore="
            + this.ignore
            + " | damage="
            + this.damage
            + " | force="
            + this.hitForce
            + " | dir=( "
            + this.hitDirectionX
            + " ; "
            + this.hitDirectionY
            + " ) ]";
    }

    void process(IsoGameCharacter isoGameCharacter1, IsoGameCharacter isoGameCharacter0) {
        isoGameCharacter0.getHitDir().set(this.hitDirectionX, this.hitDirectionY);
        isoGameCharacter0.setHitForce(this.hitForce);
        if (GameServer.bServer && isoGameCharacter0 instanceof IsoZombie && isoGameCharacter1 instanceof IsoPlayer) {
            ((IsoZombie)isoGameCharacter0).addAggro(isoGameCharacter1, this.damage);
            DebugLog.Damage
                .noise(
                    "AddAggro zombie=%d player=%d ( \"%s\" ) damage=%f",
                    isoGameCharacter0.getOnlineID(),
                    isoGameCharacter1.getOnlineID(),
                    ((IsoPlayer)isoGameCharacter1).getUsername(),
                    this.damage
                );
        }

        isoGameCharacter0.setAttackedBy(isoGameCharacter1);
    }

    public float getDamage() {
        return this.damage;
    }
}
